package com.ecode.annotation.data.json;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ecode.annotation.core.CodeDataParse;
import com.ecode.annotation.model.ErrorCodeDO;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName JsonCodeDataParse
 * @Author mjlft
 * @Date 2021/10/4 12:04
 * @Version 1.0
 * @Description json数据解析
 */
public class JsonCodeDataParse implements CodeDataParse {

    @Override
    public Set<ErrorCodeDO> parseStrToData(String srcData) {
        JSONArray jsonArray = null;
        try{
            jsonArray = JSONArray.parseArray(srcData);
        }catch (Exception e){
            throw new RuntimeException("数据转换为json格式失败 数据：->" + srcData);
        }
        int size = jsonArray.size();
        Set<ErrorCodeDO> result = new HashSet<>(size);
        for(int i = 0; i < size; i ++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            ErrorCodeDO errorCodeDO = JSONObject.toJavaObject(jsonObject, ErrorCodeDO.class);
            result.add(errorCodeDO);
        }
        return result;
    }
}
